<?php
abstract class system_view_helper_placeholder_container_abstract extends ArrayObject {
	const SET = 'SET';
	const APPEND = 'APPEND';
	const PREPEND = 'PREPEND';
	protected $_prefix = ''; //前缀
	protected $_postfix = ''; //后缀
	protected $_separator = '';
	protected $_indent = '';
	protected $_captureLock = false;
	protected $_captureType;
	protected $_captureKey;
	
	public function __construct() {
		parent::__construct(array(), parent::ARRAY_AS_PROPS);
	}
	public function set($value) {
		$this->exchangeArray(array($value));
	}
	public function prepend($value) {
		$values = $this->getArrayCopy();
		array_unshift($values, $value);
		$this->exchangeArray($values);
	}
	public function getValue() {
		if(1==count($this)) {
			$keys = $this->getKeys();
			$key = array_shift($keys);
			return $this[$key];
		}
		
		return $this->getArrayCopy();
	}
	
	/**
	 * @param  string $prefix
	 * @return Zend_View_Helper_Placeholder_Container
	 */
	public function setPrefix($prefix) {
		$this->_prefix = (string) $prefix;
		return $this;
	}
	
	/**
	 * Retrieve prefix
	 *
	 * @return string
	 */
	public function getPrefix() {
		return $this->_prefix;
	}
	
	/**
	 * Set postfix for __toString() serialization
	 *
	 * @param  string $postfix
	 * @return Zend_View_Helper_Placeholder_Container
	 */
	public function setPostfix($postfix) {
		$this->_postfix = (string) $postfix;
		return $this;
	}
	
	/**
	 * Retrieve postfix
	 *
	 * @return string
	 */
	public function getPostfix() {
		return $this->_postfix;
	}
	
	/**
	 * Set separator for __toString() serialization
	 *
	 * Used to implode elements in container
	 *
	 * @param  string $separator
	 * @return Zend_View_Helper_Placeholder_Container
	 */
	public function setSeparator($separator) {
		$this->_separator = (string) $separator;
		return $this;
	}
	
	/**
	 * Retrieve separator
	 *
	 * @return string
	 */
	public function getSeparator() {
		return $this->_separator;
	}
	
	/**
	 * Set the indentation string for __toString() serialization,
	 * optionally, if a number is passed, it will be the number of spaces
	 *
	 * @param  string|int $indent
	 * @return Zend_View_Helper_Placeholder_Container_Abstract
	 */
	public function setIndent($indent) {
		$this->_indent = $this->getWhitespace($indent);
		return $this;
	}
	
	/**
	 * Retrieve indentation
	 *
	 * @return string
	 */
	public function getIndent() {
		return $this->_indent;
	}
	
	/**
	 * Retrieve whitespace representation of $indent
	 *
	 * @param  int|string $indent
	 * @return string
	 */
	public function getWhitespace($indent) {
		if(is_int($indent)) {
			$indent = str_repeat(' ', $indent);
		}
		
		return (string) $indent;
	}
	
	/**
	 * Start capturing content to push into placeholder
	 *
	 * @param  int $type How to capture content into placeholder; append, prepend, or set
	 * @return void
	 * @throws system_view_helper_placeholder_exception if nested captures detected
	 */
	public function captureStart($type = system_view_helper_placeholder_container_abstract::APPEND, $key = null) {
		if($this->_captureLock) {
			$e = new system_view_helper_placeholder_container_exception('Cannot nest placeholder captures for the same placeholder');
			$e->setView($this->view);
			throw $e;
		}
		
		$this->_captureLock = true;
		$this->_captureType = $type;
		if((null!==$key)&&is_scalar($key)) {
			$this->_captureKey = (string) $key;
		}
		ob_start();
	}
	
	/**
	 * End content capture
	 *
	 * @return void
	 */
	public function captureEnd() {
		$data = ob_get_clean();
		$key = null;
		$this->_captureLock = false;
		if(null!==$this->_captureKey) {
			$key = $this->_captureKey;
		}
		switch($this->_captureType) {
			case self::SET:
				if(null!==$key) {
					$this[$key] = $data;
				} else {
					$this->exchangeArray(array($data));
				}
				break;
			case self::PREPEND:
				if(null!==$key) {
					$array = array($key => $data);
					$values = $this->getArrayCopy();
					$final = $array+$values;
					$this->exchangeArray($final);
				} else {
					$this->prepend($data);
				}
				break;
			case self::APPEND:
			default:
				if(null!==$key) {
					if(empty($this[$key])) {
						$this[$key] = $data;
					} else {
						$this[$key] .= $data;
					}
				} else {
					$this[$this->nextIndex()] = $data;
				}
				break;
		}
	}
	
	/**
	 * Get keys
	 *
	 * @return array
	 */
	public function getKeys() {
		$array = $this->getArrayCopy();
		return array_keys($array);
	}
	
	/**
	 * Next Index
	 *
	 * as defined by the PHP manual
	 * @return int
	 */
	public function nextIndex() {
		$keys = $this->getKeys();
		if(0==count($keys)) {
			return 0;
		}
		
		return $nextIndex = max($keys)+1;
	}
	
	/**
	 * Render the placeholder
	 *
	 * @return string
	 */
	public function toString($indent = null) {
		$indent = ($indent!==null) ? $this->getWhitespace($indent) : $this->getIndent();
		
		$items = $this->getArrayCopy();
		$return = $indent.$this->getPrefix().implode($this->getSeparator(), $items).$this->getPostfix();
		$return = preg_replace("/(\r\n?|\n)/", '$1'.$indent, $return);
		return $return;
	}
	
	/**
	 * Serialize object to string
	 *
	 * @return string
	 */
	public function __toString() {
		return $this->toString();
	}
}